package com.example.john.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private MyOpenHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper=new MyOpenHelper(this,"john1.db",null,1);

    }

    public void insert(View view) {
        SQLiteDatabase db=helper.getWritableDatabase();
        String sql="insert into teacher (id,name) values (null,?)";
        db.execSQL(sql,new Object[]{"john123"});
        db.close();

    }

    public void queryAll(View view) {
        SQLiteDatabase db=helper.getReadableDatabase();
        String sql="select * from teacher";
        Cursor c=db.rawQuery(sql,null);
        while (c.moveToNext()){
            int id=c.getInt(c.getColumnIndex("id"));
            String name=c.getString(c.getColumnIndex("name"));
            Log.i("john",id+"......"+name);
        }
        c.close();
        db.close();

    }
}
